package castledefense.gui;

import twl.global.widget.GlobalButton;

import castledefense.data.GameData;
import castledefense.logic.buildings.Building;
import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.renderer.AnimationState.StateKey;

public class UpgradeButton extends GlobalButton{
	
	public static final StateKey STATE_FIRE = StateKey.get("fire");
	public static final StateKey STATE_WATER = StateKey.get("water");
	public static final StateKey STATE_EARTH = StateKey.get("earth");
	public static final StateKey STATE_AIR = StateKey.get("air");
	
	private int type;
	private UpgradePanel panel;
	
	protected final static String[] text = {
			"Imbue your tower with power of Fire",
			"Empower your tower with Water elemental",
			"Stronger your tower with raw powers of Earth",
			"Upgrade your tower with speed of Air" };

	public UpgradeButton(String name, int type, UpgradePanel panel) {
		super(name);
		this.type = type;
		this.panel = panel;
		setSize(32, 32);
		setTheme("upgradeButtonTheme");
		
		switch(type){
		case(GameData.fire):
			getAnimationState().setAnimationState(STATE_FIRE, true);
			break;
		case(GameData.water):
			getAnimationState().setAnimationState(STATE_WATER, true);
			break;
		case(GameData.earth):
			getAnimationState().setAnimationState(STATE_EARTH, true);
			break;
		case(GameData.air):
			getAnimationState().setAnimationState(STATE_AIR, true);
			break;
		}
		
		setVisible(false);
		setTooltipContent(text[type]);
	}
	
	//---------------------- Input Event Handling ------------------------------
	
	@Override
	protected boolean handleEvent(Event evt){
		boolean result = super.handleEvent(evt);
		if(evt.getType() == Event.Type.MOUSE_BTNUP){
			if(evt.getMouseButton() == Event.MOUSE_LBUTTON){
				if(getModel().isHover()){
					isClicked();
				}
			}
		}
		return result;
	}
	
	private void isClicked() {
		// upgrade
		if (panel.getSelected() != null && panel.getSelected() instanceof Building) {
			Building building = (Building) panel.getSelected();
			building.upgrade(type);
		}
	}
	
	

}
